package com.example.ymm.util;



import ws.schild.jave.*;

import java.io.File;
import java.net.URL;

public class ConvertingAnyAudioToMp3 {


    public static void main(String[] args) {
        try{
//            File source = new File("D:\\file\\o1.ogg");
//            File target = new File("D:\\file\\o1.mp3");
//            amrToMp3(source,target);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /**
     *
     * @param source 输入文件
     * @param target 最终转换的文件
     * @throws EncoderException
     */
    public static void amrFileToMp3(File source, File target) throws EncoderException {

        //Audio Attributes
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");   //lame是一个mp3编码库

//        audio.setBitRate(128000);
//        audio.setChannels(2);
//        audio.setSamplingRate(44100);

        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);

        //Encode
        Encoder encoder = new Encoder();
        encoder.encode(new MultimediaObject(source), target, attrs);

    }


    public static void amrInputStreamToMp3(URL source, File target) throws EncoderException {

        //Audio Attributes
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");   //lame是一个mp3编码库

//        audio.setBitRate(128000);
//        audio.setChannels(2);
//        audio.setSamplingRate(44100);

        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);

        //Encode
        Encoder encoder = new Encoder();
        encoder.encode(new MultimediaObject(source), target, attrs);

    }

}
